package ai.people.platform.jx.helper;

import cn.hutool.core.lang.Pair;

import java.util.Random;

/**
 * 经纬度打散
 *
 * @author Administrator
 * @version 1.0
 * @date 2021-06-23 18:41
 */
public class TgLocationHelper {

    /**
     * 根据已知经纬度，返回radius 范围内的随机经纬度
     * @param longitude
     * @param latitude
     * @param radius
     * @return
     */
    public static Pair<Double,Double> getLocation(double longitude, double latitude, double radius) {
        Random random = new Random();

        // Convert radius from meters to degrees
        double radiusInDegrees = radius / 111000f;

        double u = random.nextDouble();
        double v = random.nextDouble();
        double w = radiusInDegrees * Math.sqrt(u);
        double t = 2 * Math.PI * v;
        double x = w * Math.cos(t);
        double y = w * Math.sin(t);

        double new_x = x / Math.cos(Math.toRadians(latitude));

        double foundLongitude = new_x + longitude;
        double foundLatitude = y + latitude;

        return Pair.of(foundLatitude, foundLongitude);
    }



}
